<template>
  <div>
    <!-- 请勿修改，（选择全部员工）  -->
    <a-modal :width="status === 6 ? 1050 : 850" title="选择员工" :visible="show" @cancel="cancel">
      <template slot="footer">
        <div style="display: flex; justify-content: space-between">
          <div style="padding-top: 4px" v-if="status !== 1">
            <a-button size="small" type="link" @click="showSenior" v-if="status !== 5"><a-icon type="search" />高级搜索</a-button>
            <a-button size="small" type="link" @click="showCopy"><a-icon type="copy" />粘贴选择</a-button>
          </div>
          <div>
            <a-button @click="cancel"> 取消 </a-button>
            <a-button key="submit" type="primary" @click="ok"> 确定 </a-button>
          </div>
        </div>
      </template>
      <a-spin :spinning="spinning">
        <div class="employee-selector-wrapper2">
          <div style="width: 260px" v-if="status === 6">
            <div style="font-size: 16px; font-weight: 500; color: #3c3d43; padding-left: 16px">定薪模板</div>
            <div style="margin-top: 12px">
              <div v-for="(item, index) in fixData" :key="index">
                <div @click="templateClick(item, index)" :class="item.hover ? 'templateHBox' : 'templateBox'">
                  {{ item.name }}
                </div>
              </div>
            </div>
          </div>
          <div class="verticalBar" v-if="status === 6" />

          <div>
            <div class="employee-selector-block">
              <a-input-search
                :placeholder="status === 6 ? '姓名/手机号/身份证号' : '输入姓名或手机号搜索员工'"
                style="width: 100%"
                @search="searchPeople"
                @change="changePeople"
              />
            </div>
            <!-- <a-tabs default-active-key="1" @change="onChangeTab" style="margin-bottom:10px;width:450px" size="small" :tabBarGutter="3">
              <a-tab-pane key="1" tab="全部待添"> -->
            <div v-if="treeData.length" class="tree-item2" style="padding-top: 24px">
              <div>全选当页</div>
              <xq-icon type="icon-wancheng2" v-if="isAllSelected" @click="choseAll(treeData)" class="iconStyle"> </xq-icon>
              <img
                v-else
                @click="choseAll(treeData)"
                src="@/assets/icons/未选择.png"
                style="width: 18px; height: 18px; margin-left: 10px"
              />
            </div>
            <div class="employee-selector">
              <div v-for="(item, index) in treeData" :key="index" class="mg_t10">
                <div class="tree-item" @click="chose(item, treeData)">
                  <span>
                    <img src="@/assets/icons/person1.png" style="width: 30px; height: 30px; margin-right: 10px" />
                    {{ item.name }}
                    {{ item.phone ? "(" + item.phone + ")" : "" }}
                  </span>
                  <xq-icon v-on:click.stop="chose(item, treeData)" type="icon-wancheng2" v-if="item.isSelected" class="iconStyle">
                  </xq-icon>
                  <img v-else v-on:click.stop="chose(item, treeData)" src="@/assets/icons/未选择.png" style="width: 18px; height: 18px" />
                </div>
              </div>
            </div>
            <a-pagination
              v-model="current"
              size="small"
              :page-size-options="pageSizeOptions"
              :total="total"
              show-size-changer
              show-less-items
              :page-size="pageSize"
              @showSizeChange="onShowSizeChange"
              @change="onPageChange"
            >
              <template slot="buildOptionText" slot-scope="props">
                <span>{{ props.value }}条/页</span>
              </template>
            </a-pagination>
            <!-- </a-tab-pane>
            </a-tabs> -->
          </div>
          <div class="verticalBar" />

          <div class="employee-selector-block2">
            <div class="employee-selector-title">已经选择人员 {{ selectedListTemp.length }}人</div>
            <div class="employee-selector">
              <div v-for="(item, i) in selectedListTemp" :key="i" class="employee-selector-item">
                <div>
                  <img src="@/assets/icons/person1.png" style="width: 30px; height: 30px; margin-right: 10px" />
                  {{ item.name }}
                </div>
                <img @click="deleteItem(item, i)" src="@/assets/icons/删除.png" style="width: 20px; height: 20px" />
              </div>
            </div>
          </div>
        </div>
      </a-spin>
    </a-modal>
    <a-modal title="适用范围-粘贴选择" :width="850" :visible="copyVisible" @cancel="copyVisible = false" @ok="copyOk">
      <div class="flex">
        <div style="width: 65%">
          <div style="width: 100%; height: 125px; background: #faf9f9; border-radius: 4px; padding: 14px">
            <div style="font-weight:400;color:#252525line-height:18px;font-size:16px">温馨提示</div>
            <div style="font-weight: 400; color: #999999; line-height: 18px; font-size: 12px; margin-top: 8px">
              1、在文本框内输入或批量粘贴企业在职员工身份证号，便可识别添加。
            </div>
            <div
              v-if="status !== 6 && status !== 7"
              style="font-weight: 400; color: #999999; line-height: 18px; font-size: 12px; margin-top: 8px"
            >
              2、非企业员工<span v-if="status !== 4">、已离职员工</span>无法识别。
            </div>
            <div v-if="status === 6" style="font-weight: 400; color: #999999; line-height: 18px; font-size: 12px; margin-top: 8px">
              2、仅识别固定薪资档案中的人员
            </div>
            <div v-if="status === 7" style="font-weight: 400; color: #999999; line-height: 18px; font-size: 12px; margin-top: 8px">
              2、仅识别工价薪资档案中的人员
            </div>
            <div style="font-weight: 400; color: #999999; line-height: 18px; font-size: 12px; margin-top: 8px">
              3、支持以换行、回车、空格、逗号等符号隔开的身份证号识别
            </div>
          </div>
          <div style="margin-top: 12px">
            <a-textarea placeholder="请输入或粘贴员工身份证号" v-model="textContent" :rows="15" />
          </div>
          <div style="padding-top: 12px; display: flex; justify-content: flex-end">
            <div v-if="errorState" style="color: #ff0000; margin-right: 130px">
              <a-icon style="font-size: 16px; margin-right: 8px" type="exclamation-circle" />文本框内存在无法识别的身份证号
            </div>
            <a-button style="margin-right: 12px" @click="reset">重置</a-button>
            <a-button @click="getEmpListByIdCards" :disabled="textContent === '' || errorState">识别</a-button>
          </div>
        </div>

        <a-divider type="vertical" style="height: 480px" />
        <div style="width: 35%">
          <div>
            已选择的员工 <span class="count">{{ chosenList.length }}人</span>
          </div>
          <div style="height: 450px; overflow: scroll">
            <div style="padding: 8px; display: flex; justify-content: space-between" v-for="(item, index) in chosenList" :key="index">
              {{ item.name }}
              <img src="@/assets/icons/删除.png" style="width: 20px; height: 20px; cursor: pointer" @click="deleteCopyItem(item)" />
            </div>
          </div>
        </div>
      </div>
    </a-modal>
    <a-modal title="适用范围-高级筛选" :width="1000" :visible="seniorVisible" @cancel="seniorCancel" @ok="seniorOk">
      <div style="display: flex">
        <div style="width: 35%">
          <a-form-model :model="form" :label-col="labelCol" :wrapper-col="wrapperCol">
            <a-form-model-item label="筛选条件">
              <a-input
                v-model="form.name"
                :placeholder="status === 6 || status === 4 || status === 7 ? '姓名/手机号/身份证号' : '姓名/手机号'"
              />
            </a-form-model-item>
            <a-form-model-item label="员工标签">
              <a-select mode="multiple" option-label-prop="label" v-model="form.tag" placeholder="请选择">
                <a-select-option v-for="item in labelList" :key="item.tagName" :value="item.tagName" :label="item.tagName">{{
                  item.tagName
                }}</a-select-option>
              </a-select>
            </a-form-model-item>
            <a-form-model-item label="工作性质">
              <a-select allowClear placeholder="请选择" v-model="form.postType">
                <a-select-option v-for="(item, index) in postList" :key="index" :value="item.key">{{ item.value }}</a-select-option>
              </a-select>
            </a-form-model-item>
            <a-form-model-item label="入职日期" v-if="status !== 7">
              <a-range-picker @change="rangeChange" v-model="form.date" />
            </a-form-model-item>
            <a-form-model-item label="工价规则" v-if="status !== 3 && status !== 4 && status !== 6">
              <a-select
                allowClear
                show-search
                option-filter-prop="children"
                :filter-option="filterOption"
                placeholder="请选择"
                v-model="form.rule"
              >
                <a-select-option v-for="(item, index) in rulusList" :key="index" :value="item.id">{{ item.name }}</a-select-option>
              </a-select>
            </a-form-model-item>
            <a-form-model-item label="劳务费规则" v-if="status !== 3 && status !== 4 && status !== 6">
              <a-select
                allowClear
                show-search
                option-filter-prop="children"
                :filter-option="filterOption"
                placeholder="请选择"
                v-model="form.serviceFeeRuleId"
              >
                <a-select-option v-for="(item, index) in serviceFeeRuleList" :key="index" :value="item.id">{{ item.name }}</a-select-option>
              </a-select>
            </a-form-model-item>
            <a-form-model-item label="渠道来源" v-if="status === 7 || status === 4">
              <a-select
                allowClear
                show-search
                option-filter-prop="children"
                :filter-option="filterOption"
                placeholder="请选择渠道"
                v-model="form.channelId"
              >
                <a-select-option v-model="item.id" v-for="(item, index) in Channels" :key="index">
                  {{ item.name }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-form-model>
          <div style="display: flex; justify-content: center">
            <a-button @click="clearForm">重置</a-button
            ><a-button type="primary" style="margin-left: 8px" @click="getSeniorListCheck">查询</a-button>
          </div>
        </div>
        <a-divider type="vertical" style="height: 480px" />
        <div style="width: 40%">
          <a-table
            :columns="columns"
            :data-source="dataList"
            :pagination="pagination"
            @change="handleTableChange"
            :scroll="{ x: 500 }"
            row-key="id"
            :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
          >
          </a-table>
        </div>

        <a-divider type="vertical" style="height: 480px" />
        <div style="width: 25%">
          <div>
            已选择的员工 <span class="count">{{ seniorList.length }}人</span>
          </div>
          <div style="height: 450px; overflow: scroll">
            <div style="padding: 8px; display: flex; justify-content: space-between" v-for="(item, index) in seniorList" :key="index">
              {{ item.name }}
              <img src="@/assets/icons/删除.png" style="width: 20px; height: 20px; cursor: pointer" @click="deleteSeniorItem(item)" />
            </div>
          </div>
        </div>
      </div>
    </a-modal>
  </div>
</template>

<script>
import {
  getDepEmpInfo,
  getAllEmp,
  getAllEmp2,
  empListByIdCards,
  listByCompanyIdGet,
  empListByIdCards2,
  salaryCalculateRulesListall,
  empListByIdCardsRYK,
  empListByIdCardsTEM
} from "@/api/company/contract";
import { getSalaryCalculateRuleGroups } from "@/api/outsourcing/outrules";
import { getNotList, getPeopleList, selectOutsourceSalary, getmpByCardAndSalaryType, isSameTemplate, getAllList } from "./api";
import { selectByCompanyId } from "@/api/wages/file";
import { getWageEmp, getWageEmp2 } from "@/api/company/contract";
import moment from "moment";
const columns = [
  { title: "姓名", dataIndex: "name", key: "name", width: 80 },
  { title: "手机号", dataIndex: "phone", key: "phone", width: 120 },
  { title: "身份证号", dataIndex: "idCard", key: "idCard", width: 180 }
];
export default {
  props: {
    status: {
      type: Number,
      default: 2,
      require: true
    },
    yearly: {
      type: String,
      default: "",
      require: true
    },
    monthly: {
      type: String,
      default: "",
      require: true
    },
    show: {
      type: Boolean,
      default: false
    },
    number: {
      type: Number,
      default: 0
    },
    templateId: {
      type: Number,
      default: 0
    },
    defaultSelectedKeys: {
      type: Array,
      default: () => []
    },
    selectedList: {
      type: Array,
      default: () => []
    },
    isOnlineOrLeave: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      seniorList: [],
      dataList: [],
      fixData: [],
      nameOrPhone: "",
      columns,
      spinning: false,
      selectedRowKeys: [],
      copyVisible: false,
      importDataLength: 0,
      labelCol: { span: 7 },
      wrapperCol: { span: 16 },
      mode: "1",
      form: {},
      labelList: [],
      treeData: [],
      errorState: false,
      selectedListTemp: [],
      chosenList: [],
      serviceFeeRuleList: [],
      rulusList: [],
      isAllSelected: false,
      seniorVisible: false,
      checkStatue: 0,
      textContent: "",
      selectedKeysBeforeSearch: [],
      postTypeValue: 3,
      pageSizeOptions: ["10", "20", "50", "100"],
      pageSize: 10,
      current: 1,
      templateItemId: "",
      Channels: [],
      total: 50,
      pagination: {
        pageSize: 10,
        total: 0,
        size: "small",
        current: 1,
        showTotal: (total) => `共 ${total} 条`
      }
    };
  },
  computed: {
    postTypeList() {
      return this.$store.state.enums.postTypeLst;
    },
    postList() {
      return this.postTypeList.map((item) => ({
        value: item.title,
        key: item.value
      }));
    }
  },
  watch: {
    textContent(e) {
      if (e) {
        this.errorState = false;
      }
    },

    show(val) {
      if (val) {
        this.selectedListTemp = JSON.parse(JSON.stringify(this.selectedList));
        this.treeData = [];
        this.current = 1;
        this.postTypeValue = 3;
        this.mode = "1";
        this.importDataLength = this.selectedList.length;
        // this.onPageChange(1);
        this.spinning = false;

        if (this.status === 6) {
          this.initTemplate();
        } else {
          this.getSelectDepTree();
        }
      }
    },

    month(val) {}
  },
  created() {
    this.getAllList();
  },
  methods: {
    getAllList() {
      this.loading = true;
      getAllList().then((res) => {
        // this.getPrePersonnelList();
        this.Channels = res.data;
      });
    },
    templateClick(e, index) {
      this.fixData.forEach((item2, index2) => {
        if (index2 === index) {
          this.fixData[index2].hover = true;
          this.templateItemId = this.fixData[index2].id;
          this.pageSize = 10;
          this.current = 1;
          this.getSelectDepTree();
        } else {
          this.fixData[index2].hover = false;
        }
      });
      this.fixData = JSON.parse(JSON.stringify(this.fixData));
      console.log(this.fixData);
    },
    initTemplate() {
      selectByCompanyId({ companyId: this.$store.state.hr.companyId })
        .then((res) => {
          this.fixData = res.data;
          this.fixData[0].hover = true;
          this.templateItemId = this.fixData[0].id;
          this.pageSize = 10;
          this.current = 1;
          this.getSelectDepTree();
        })
        .catch((err) => {
          console.log("debug log --> ", err);
        });
    },
    rangeChange(e) {
      this.form.joinDateStart = e[0];
      this.form.joinDateEnd = e[1];
    },
    filterOption(input, option) {
      return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0;
    },
    deleteSeniorItem(e) {
      this.seniorList.forEach((item, index) => {
        if (item.id === e.id) {
          this.seniorList.splice(index, 1);
          this.selectedRowKeys.forEach((item2, index2) => {
            if (item2 === e.id) {
              this.selectedRowKeys.splice(index2, 1);
            }
          });
        }
      });
    },
    onSelectChange(selectedRowKeys, e) {
      this.selectedRowKeys = selectedRowKeys;
      this.dataList.forEach((item) => {
        selectedRowKeys.forEach((item2) => {
          if (item.id === item2) {
            this.seniorList.push(item);
            this.seniorList = this.unique(this.seniorList);
          }
        });
      });
      // this.seniorList.forEach((item, index) => {
      //   if (selectedRowKeys.indexOf(item.id) === -1) {
      //     this.seniorList.splice(index, 1);
      //   }
      // });
      this.seniorList.forEach((item, index) => {
        if (this.selectedRowKeys.indexOf(item.id) === -1) {
          item.isSelectedItem = 2;
        }
      });
      var arr = [];
      this.seniorList.forEach((item, index) => {
        if (!item.isSelectedItem) {
          arr.push(item);
        }
      });
      this.seniorList = JSON.parse(JSON.stringify(arr));
    },
    unique(arr) {
      const res = new Map();
      return arr.filter((arr) => !res.has(arr.id) && res.set(arr.id, 1));
    },
    handleTableChange(pagination, filters, sorter) {
      this.pagination.current = pagination.current;
      this.pagination.pageSize = pagination.pageSize;
      // this.checkStatue = 0;
      this.getSeniorList();
    },
    getSeniorListCheck() {
      this.pagination.pageSize = 10;
      this.pagination.current = 1;
      this.checkStatue = 1;
      this.getSeniorList();
    },
    getSeniorList() {
      var params = {
        pageNo: this.pagination.current,
        pageSize: this.pagination.pageSize,
        companyId: this.$store.state.hr.companyId
      };
      if (this.checkStatue === 1) {
        params = {
          // onlyCurrentDep: true,
          // onlyUser: true,
          salaryRuleId: this.form.rule ? this.form.rule : null,
          serviceFeeRuleId: this.form.serviceFeeRuleId ? this.form.serviceFeeRuleId : null,
          postType: this.form.postType ? this.form.postType : null,
          tag: this.form.tag ? this.form.tag : null,
          nameOrPhone: this.form.name ? this.form.name : null,
          joinDateStart: this.form.joinDateStart ? moment(this.form.joinDateStart).format("YYYY-MM-DD ") : null,
          joinDateEnd: this.form.joinDateEnd ? moment(this.form.joinDateEnd).format("YYYY-MM-DD ") : null,
          pageNo: this.pagination.current,
          pageSize: this.pagination.pageSize,
          companyId: this.$store.state.hr.companyId,
          channelId: this.form.channelId ? this.form.channelId : null
        };
      }
      if (this.status === 2 || this.status === 3) {
        getAllEmp(params)
          .then((res) => {
            this.spinning = false;
            if (res.data.entities === null) {
              this.total = 0;
            } else {
              this.pagination.total = res.data && res.data.entityCount;
              this.dataList = res.data.entities;
            }
          })
          .catch((err) => {
            console.log("debug log --> ", err);
          });
      } else if (this.status === 1) {
        getAllEmp2({ ...params, monthly: this.monthly, yearly: this.yearly })
          .then((res) => {
            if (res.data.entities === null) {
              this.total = 0;
            } else {
              this.pagination.total = res.data && res.data.entityCount;
              this.dataList = res.data.entities;
            }
          })
          .catch((err) => {
            console.log("debug log --> ", err);
          });
      } else if (this.status === 4) {
        getNotList(params)
          .then((res) => {
            this.spinning = false;
            if (res.data.entities === null) {
              this.total = 0;
            } else {
              this.pagination.total = res.data && res.data.entityCount;
              this.dataList = res.data.entities;
            }
          })
          .catch((err) => {
            console.log("debug log --> ", err);
          });
      } else if (this.status === 6) {
        params.name = params.nameOrPhone;
        params.nameOrPhone = undefined;
        getWageEmp2(params)
          .then((res) => {
            this.spinning = false;
            if (res.data.entities === null) {
              this.total = 0;
            } else {
              this.pagination.total = res.data && res.data.entityCount;
              this.dataList = res.data.entities;
            }
          })
          .catch((err) => {
            console.log("debug log --> ", err);
          });
      } else if (this.status === 7) {
        selectOutsourceSalary(params)
          .then((res) => {
            this.spinning = false;
            if (res.data.entities === null) {
              this.total = 0;
            } else {
              this.pagination.total = res.data && res.data.entityCount;
              this.dataList = res.data.entities;
            }
          })
          .catch((err) => {
            console.log("debug log --> ", err);
          });
      }
    },
    clearForm() {
      this.form = {};
    },
    seniorCancel() {
      this.seniorVisible = false;
      this.selectedRowKeys = [];
    },
    showSenior() {
      this.checkStatue = 0;
      this.$emit("update:show", false);
      this.seniorVisible = true;
      this.clearForm();
      this.getSeniorList();
      this.getListByCompanyId();
      this.update();
      // setTimeout(function() {
      if (this.selectedListTemp.length > 0) {
        this.seniorList = JSON.parse(JSON.stringify(this.selectedListTemp));
        this.selectedListTemp.forEach((item, index) => {
          this.selectedRowKeys.push(item.id);
        });
      } else {
        this.selectedRowKeys = [];
        this.seniorList = [];
      }
      // }, 1000);
    },
    showCopy() {
      this.$emit("update:show", false);
      this.copyVisible = true;
      if (this.selectedListTemp.length > 0) {
        this.chosenList = JSON.parse(JSON.stringify(this.selectedListTemp));
      } else {
        this.chosenList = [];
      }
    },
    deleteCopyItem(e) {
      this.chosenList.forEach((item, index) => {
        if (item.id === e.id) {
          this.chosenList.splice(index, 1);
        }
      });
    },
    reset() {
      this.textContent = "";
      this.errorState = false;
    },
    getEmpListByIdCards() {
      if (this.textContent !== "") {
        var arr = [];
        arr = this.textContent.split(/[\W]/);
        if (this.status === 4 || this.status === 1) {
          empListByIdCards2(this.$store.state.hr.companyId, arr).then((res) => {
            if (this.selectedListTemp.length > 0) {
              this.chosenList = res.data.empInfoVOList.concat(this.selectedListTemp);
              this.chosenList = this.unique(this.chosenList);
            } else {
              this.chosenList = res.data.empInfoVOList;
            }
            this.textContent = res.data.idCardList.join(",");
            if (res.data.idCardList.length > 0) {
              this.$nextTick(() => {
                this.errorState = true;
              });
            }
          });
        } else if (this.status === 5) {
          empListByIdCardsRYK({ idCardsList: arr }).then((res) => {
            if (this.selectedListTemp.length > 0) {
              this.chosenList = this.chosenList.concat(res.data.transferAccountsPeopleVOList.concat(this.selectedListTemp));
              this.chosenList = this.unique(this.chosenList);
            } else {
              this.chosenList = this.chosenList.concat(res.data.transferAccountsPeopleVOList);
            }
            this.textContent = res.data.idCardList.join(",");
            if (res.data.idCardList.length > 0) {
              this.$nextTick(() => {
                this.errorState = true;
              });
            }
          });
        } else if (this.status === 6) {
          empListByIdCardsTEM({ companyId: this.$store.state.hr.companyId, idCardList: arr }).then((res) => {
            // if (this.selectedListTemp.length > 0) {
            this.chosenList = res.data.empInfoListVOList.concat(this.selectedListTemp);
            this.chosenList = this.unique(this.chosenList);
            // } else {
            //   this.chosenList = res.data.empInfoVOList;
            // }
            this.textContent = res.data.idCardList.join(",");
            if (res.data.idCardList.length > 0) {
              this.$nextTick(() => {
                this.errorState = true;
              });
            }
          });
        } else if (this.status === 7) {
          getmpByCardAndSalaryType({ companyId: this.$store.state.hr.companyId, idCardList: arr, salaryType: 2 }).then((res) => {
            // if (this.selectedListTemp.length > 0) {
            if (res.data.empInfoVOList && res.data.empInfoVOList.length) {
              this.chosenList = res.data.empInfoVOList.concat(this.selectedListTemp);
              this.chosenList = this.unique(this.chosenList);
            }

            // } else {
            //   this.chosenList = res.data.empInfoVOList;
            // }
            this.textContent = res.data.idCardList.join(",");
            if (res.data.idCardList.length > 0) {
              this.$nextTick(() => {
                this.errorState = true;
              });
            }
          });
        } else {
          empListByIdCards(this.$store.state.hr.companyId, arr).then((res) => {
            if (this.selectedListTemp.length > 0) {
              this.chosenList = res.data.empInfoVOList.concat(this.selectedListTemp);
              this.chosenList = this.unique(this.chosenList);
            } else {
              this.chosenList = res.data.empInfoVOList;
            }
            this.textContent = res.data.idCardList.join(",");
            if (res.data.idCardList.length > 0) {
              this.$nextTick(() => {
                this.errorState = true;
              });
            }
          });
        }
      }
    },
    // 分页变化
    update() {
      this.rulusList = [];
      this.serviceFeeRuleList = [];
      getSalaryCalculateRuleGroups({
        companyId: this.$store.state.hr.companyId
      }).then((res) => {
        if (res.data.length > 0) {
          res.data.map((item) => {
            if (item.ruleType === 1) {
              this.rulusList.push(item);
            }
            if (item.ruleType === 2) {
              this.serviceFeeRuleList.push(item);
            }
          });
        }
      });
    },
    onPageChange(pageNumber) {
      this.current = pageNumber;
      this.getSelectDepTree();
    },
    postTypeClick(value) {
      this.postTypeValue = value;
      this.isAllSelected = false;
      this.current = 1;
      this.getSelectDepTree();
    },
    onShowSizeChange(current, pageSize) {
      this.pageSize = pageSize;
      this.current = 1;
      this.getSelectDepTree();
    },
    choseAll(data) {
      this.isAllSelected = !this.isAllSelected;
      var that = this;
      this.spinning = true;
      for (var j = 0, len = data.length; j < len; j++) {
        data[j].isSelected = that.isAllSelected;
        that.ergodicAll(data[j]);
      }
      setTimeout(function () {
        that.spinning = false;
      }, "10");
    },
    chose(item, data) {
      item.isSelected = !item.isSelected;
      this.onChange(item);
    },
    onChangeTab(e) {
      this.isAllSelected = false;
      this.current = 1;
      this.mode = e;
      this.getSelectDepTree();
    },
    onChange(item) {
      this.spinning = true;
      var that = this;
      setTimeout(function () {
        that.$nextTick(() => {
          that.ergodic(item);
        });

        that.spinning = false;
      }, "10");
    },
    getListByCompanyId() {
      listByCompanyIdGet({ companyId: this.$store.state.hr.companyId })
        .then(({ data }) => {
          this.labelList = data;
        })
        .catch((err) => {
          console.log("debug log --> ", err);
        });
    },
    ergodic(item) {
      if (!item.isSelected) {
        var index = this.selectedListTemp.findIndex((it) => it.id === item.id); // 存在再删除，防止删除错误
        if (index !== -1) {
          this.selectedListTemp.splice(index, 1); // 删除

          this.treeData.some((e) => {
            if (e.id === item.id) {
              e.isSelected = false;
              return true;
            }
          });
          var allState = true;
          this.treeData.forEach((item, index) => {
            if (item.isSelected === false) {
              allState = false;
            }
          });
          this.isAllSelected = allState;
        }
      } else {
        if (this.selectedListTemp.findIndex((it) => it.id === item.id) === -1) {
          // 不存在再添加，防止重复添加
          this.selectedListTemp.push(item);
          var count = this.treeData.length;
          var allSelect = true;
          for (var o = 0; o < count; o++) {
            if (!this.treeData[o].isSelected) {
              allSelect = false;
              break;
            }
          }
          this.isAllSelected = allSelect;
        }
      }
    },
    ergodicAll(item) {
      if (!item.isSelected) {
        var index = this.selectedListTemp.findIndex((it) => it.id === item.id); // 存在再删除，防止删除错误
        if (index !== -1) {
          this.selectedListTemp.splice(index, 1); // 删除
        }
      } else {
        if (this.selectedListTemp.findIndex((it) => it.id === item.id) === -1) {
          // 不存在再添加，防止重复添加
          this.selectedListTemp.push(item);
        }
      }
    },

    // 获取部门树

    getSelectDepTree(query) {
      if (query) {
        this.current = 1;
      }
      const params = {
        // onlyCurrentDep: true,
        // onlyUser: true,
        pageSize: this.pageSize,
        pageNo: this.current,
        companyId: this.$store.state.hr.companyId
      };

      if (this.nameOrPhone) {
        params["nameOrPhone"] = this.nameOrPhone;
      }

      this.spinning = true;
      if (this.status === 2 || this.status === 3) {
        getAllEmp(params)
          .then((res) => {
            this.spinning = false;
            if (res.data.entities === null) {
              this.$message.success("暂无数据");
              this.treeData = [];
              this.total = 0;
              this.isAllSelected = false;
            } else {
              this.treeData = this.formatCompanyData(res.data.entities);
              this.total = res.data.entityCount;
              var count = res.data.entities.length;
              this.pageSize = res.data.pageSize;
              this.current = res.data.pageNo;
              var allSelect = true;
              this.spinning = false;

              if (count !== 0) {
                for (var i = 0; i < count; i++) {
                  if (!this.treeData[i].isSelected) {
                    allSelect = false;
                    break;
                  }
                }

                this.isAllSelected = allSelect;
              } else {
                this.isAllSelected = false;
              }
            }
          })
          .catch((err) => {
            console.log("debug log --> ", err);
            this.spinning = false;
          });
      } else if (this.status === 4) {
        getNotList(params)
          .then((res) => {
            this.spinning = false;
            if (res.data.entities === null) {
              this.$message.success("暂无数据");
              this.treeData = [];
              this.total = 0;
              this.isAllSelected = false;
            } else {
              this.treeData = this.formatCompanyData(res.data.entities);
              this.total = res.data.entityCount;
              var count = res.data.entities.length;
              this.pageSize = res.data.pageSize;
              this.current = res.data.pageNo;
              var allSelect = true;
              this.spinning = false;

              if (count !== 0) {
                for (var i = 0; i < count; i++) {
                  if (!this.treeData[i].isSelected) {
                    allSelect = false;
                    break;
                  }
                }

                this.isAllSelected = allSelect;
              } else {
                this.isAllSelected = false;
              }
            }
          })
          .catch((err) => {
            console.log("debug log --> ", err);
            this.spinning = false;
          });
      } else if (this.status === 1) {
        getAllEmp2({ ...params, monthly: this.monthly, yearly: this.yearly })
          .then((res) => {
            this.spinning = false;
            if (res.data.entities === null) {
              this.$message.success("暂无数据");
              this.treeData = [];
              this.total = 0;
              this.isAllSelected = false;
            } else {
              this.treeData = this.formatCompanyData(res.data.entities);
              this.total = res.data.entityCount;
              var count = res.data.entities.length;
              this.pageSize = res.data.pageSize;
              this.current = res.data.pageNo;
              var allSelect = true;
              this.spinning = false;

              if (count !== 0) {
                for (var i = 0; i < count; i++) {
                  if (!this.treeData[i].isSelected) {
                    allSelect = false;
                    break;
                  }
                }

                this.isAllSelected = allSelect;
              } else {
                this.isAllSelected = false;
              }
            }
          })
          .catch((err) => {
            console.log("debug log --> ", err);
            this.spinning = false;
          });
      } else if (this.status === 5) {
        params.companyId = undefined;
        getPeopleList({ nameOrPhoneOrIdCard: this.nameOrPhone, pageSize: this.pageSize, pageNo: this.current })
          .then((res) => {
            this.spinning = false;
            if (res.data.entities === null) {
              this.$message.success("暂无数据");
              this.treeData = [];
              this.total = 0;
              this.isAllSelected = false;
            } else {
              this.treeData = this.formatCompanyData(res.data.entities);
              this.total = res.data.entityCount;
              var count = res.data.entities.length;
              this.pageSize = res.data.pageSize;
              this.current = res.data.pageNo;
              var allSelect = true;
              this.spinning = false;

              if (count !== 0) {
                for (var i = 0; i < count; i++) {
                  if (!this.treeData[i].isSelected) {
                    allSelect = false;
                    break;
                  }
                }

                this.isAllSelected = allSelect;
              } else {
                this.isAllSelected = false;
              }
            }
          })
          .catch((err) => {
            console.log("debug log --> ", err);
            this.spinning = false;
          });
      } else if (this.status === 6) {
        params.name = params.nameOrPhone;
        params.nameOrPhone = undefined;
        getWageEmp({ ...params, salaryTemplateId: this.templateItemId })
          .then((res) => {
            this.spinning = false;
            if (res.data.entities === null) {
              this.$message.success("暂无数据");
              this.treeData = [];
              this.total = 0;
              this.isAllSelected = false;
            } else {
              this.treeData = this.formatCompanyData(res.data.entities);
              this.total = res.data.entityCount;
              var count = res.data.entities.length;
              this.pageSize = res.data.pageSize;
              this.current = res.data.pageNo;
              var allSelect = true;
              this.spinning = false;

              if (count !== 0) {
                for (var i = 0; i < count; i++) {
                  if (!this.treeData[i].isSelected) {
                    allSelect = false;
                    break;
                  }
                }

                this.isAllSelected = allSelect;
              } else {
                this.isAllSelected = false;
              }
            }
          })
          .catch((err) => {
            console.log("debug log --> ", err);
            this.spinning = false;
          });
      } else if (this.status === 7) {
        selectOutsourceSalary(params)
          .then((res) => {
            this.spinning = false;
            if (res.data.entities === null) {
              this.$message.success("暂无数据");
              this.treeData = [];
              this.total = 0;
              this.isAllSelected = false;
            } else {
              this.treeData = this.formatCompanyData(res.data.entities);
              this.total = res.data.entityCount;
              var count = res.data.entities.length;
              this.pageSize = res.data.pageSize;
              this.current = res.data.pageNo;
              var allSelect = true;
              this.spinning = false;

              if (count !== 0) {
                for (var i = 0; i < count; i++) {
                  if (!this.treeData[i].isSelected) {
                    allSelect = false;
                    break;
                  }
                }

                this.isAllSelected = allSelect;
              } else {
                this.isAllSelected = false;
              }
            }
          })
          .catch((err) => {
            console.log("debug log --> ", err);
            this.spinning = false;
          });
      }
    },
    formatCompanyData(company) {
      const parent = [];
      for (var key = 0; key < company.length; key++) {
        var selected = false;

        this.selectedListTemp.forEach((item, index) => {
          if (item.id === company[key].id) {
            selected = true;
          }
        });
        var emp = {
          id: company[key].id,
          name: company[key].name,
          phone: company[key].phone,
          idCard: company[key].idCard,
          depName: company[key].depName,
          positionName: company[key].positionName,
          isSelected: selected
        };
        // this.selectedListTemp = this.selectedList;
        this.selectedListTemp.forEach((item, index) => {
          item.isSelected = true;
        });

        parent.push(emp);
      }
      this.importDataLength = 0; // 清空传入的默认值
      return parent;
    },
    searchPeople(query) {
      this.pageSize = 10;
      this.current = 1;
      this.nameOrPhone = query;

      this.selectedKeysBeforeSearch = this.selectedListTemp.map((item) => item.id);
      this.getSelectDepTree(query);
    },
    changePeople(e) {
      if (e.target.value === "") {
        this.pageSize = 10;
        this.current = 1;
        this.getSelectDepTree();
        this.selectedKeysBeforeSearch = this.selectedListTemp.map((item) => item.id);
      }
    },
    deleteItem(item, index) {
      this.spinning = true;
      item.isSelected = false;
      var that = this;
      setTimeout(function () {
        that.$nextTick(() => {
          that.ergodic(item);
        });

        that.spinning = false;
      }, 1000);
    },
    copyOk() {
      if (this.chosenList.length === 0) {
        this.$message.success("请添加选择人员");
        return;
      } else if (this.status === 6) {
        var arr = [];
        this.chosenList.forEach((item) => {
          arr.push(item.id);
        });
        isSameTemplate({
          empIds: arr
        })
          .then(({ success }) => {
            if (success) {
              this.$emit("update:selectedList", this.chosenList);
              this.$emit(
                "update:selectThree",
                this.chosenList
                  .slice(0, 3)
                  .map((item) => item.name)
                  .join(",")
              );
              this.copyVisible = false;
              this.$emit("change", this.chosenList);
            }
          })
          .catch((err) => {
            console.log(err);
          });
      } else {
        this.$emit("update:selectedList", this.chosenList);
        this.$emit(
          "update:selectThree",
          this.chosenList
            .slice(0, 3)
            .map((item) => item.name)
            .join(",")
        );
        this.copyVisible = false;
        this.$emit("change", this.chosenList);
      }
    },
    seniorOk() {
      if (this.seniorList.length === 0) {
        this.$message.success("请添加选择人员");
        return;
      } else if (this.status === 6) {
        var arr = [];
        this.seniorList.forEach((item) => {
          arr.push(item.id);
        });
        isSameTemplate({
          empIds: arr
        })
          .then(({ success }) => {
            if (success) {
              this.$emit("update:selectedList", this.seniorList);
              this.$emit(
                "update:selectThree",
                this.seniorList
                  .slice(0, 3)
                  .map((item) => item.name)
                  .join(",")
              );
              this.seniorVisible = false;
              this.$emit("change", this.seniorList);
            }
          })
          .catch((err) => {
            console.log(err);
          });
      } else {
        this.$emit("update:selectedList", this.seniorList);
        this.$emit(
          "update:selectThree",
          this.seniorList
            .slice(0, 3)
            .map((item) => item.name)
            .join(",")
        );
        this.seniorVisible = false;
        this.$emit("change", this.seniorList);
      }
    },

    ok() {
      if (this.selectedListTemp.length === 0) {
        this.$message.success("请选择人员");
        return;
      } else if (this.status === 6) {
        var arr = [];
        this.selectedListTemp.forEach((item) => {
          arr.push(item.id);
        });
        isSameTemplate({
          empIds: arr
        })
          .then(({ success }) => {
            if (success) {
              this.$emit("update:selectedList", this.selectedListTemp);
              this.$emit(
                "update:selectThree",
                this.selectedListTemp
                  .slice(0, 3)
                  .map((item) => item.name)
                  .join(",")
              );
              this.$emit("update:show", false);
              this.$emit("change", this.selectedListTemp);
            }
          })
          .catch((err) => {
            console.log(err);
          });
      } else {
        this.$emit("update:selectedList", this.selectedListTemp);
        this.$emit(
          "update:selectThree",
          this.selectedListTemp
            .slice(0, 3)
            .map((item) => item.name)
            .join(",")
        );
        this.$emit("update:show", false);
        this.$emit("change", this.selectedListTemp);
      }
    },
    cancel() {
      this.$emit("update:show", false);
    }
  }
};
</script>
<style lang="scss" scoped>
.templateBox {
  width: 240px;
  height: 40px;
  display: flex;
  align-items: center;
  border-radius: 4px;
  font-size: 14px;
  font-weight: 400;
  color: #55565d;
  padding-left: 16px;
  cursor: pointer;
}
.templateHBox {
  width: 240px;
  height: 40px;
  display: flex;
  align-items: center;
  border-radius: 4px;
  font-size: 14px;
  font-weight: 400;
  color: #55565d;
  padding-left: 16px;
  cursor: pointer;
  background: var(--primary10);
}
.templateBox:hover {
  background: var(--primary10);
}
.postTypeL {
  display: flex;

  flex-direction: row;
  align-items: center;
}
.employee-selector {
  height: 410px;
  margin-top: 2px;
  padding-right: 10px;
  margin-bottom: 10px;
  overflow: auto;
}
.verticalBar {
  width: 1px;
  background: #e8e8e8;
  display: inline-block;
  margin-top: 40px;
  vertical-align: top;
  margin-right: 30px;
  margin-left: 30px;
  html,
  body,
  #app {
    height: 100%;
  }
}
.tree-item {
  display: flex;
  justify-content: space-between;
  flex-direction: row;
  align-items: center;
  cursor: pointer;
  margin-top: 10px;
}
.tree-item2 {
  display: flex;
  flex-direction: row;
  align-items: center;
  cursor: pointer;
  // margin-top: 10px;
  margin-bottom: 10px;
}
.employee-selector-wrapper2 {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  .employee-selector-block {
    width: 360px;
    .employee-selector-title {
      width: 100%;
      height: 32px;
      line-height: 32px;
      font-size: 14px;
    }
    .employee-selector {
      height: 400px;
      margin-top: 20px;
      padding-right: 20px;
      overflow: auto;
      .employee-selector-item {
        width: 100%;
        display: flex;
        flex-direction: row;
        justify-content: space-between;
        align-items: center;
        margin-top: 10px;
      }
    }
  }
  .employee-selector-block2 {
    width: 400px;
    .employee-selector-title {
      width: 100%;
      height: 32px;
      line-height: 32px;
      font-size: 14px;
    }
    .employee-selector {
      height: 500px;
      margin-top: 20px;
      padding-right: 10px;
      overflow: auto;
      .employee-selector-item {
        width: 100%;
        display: flex;
        flex-direction: row;
        justify-content: space-between;
        align-items: center;
        margin-top: 10px;
      }
    }
  }
}
.iconStyle {
  margin-left: 10px;
  font-size: 18px;
  color: var(--primary);
}
.count {
  color: var(--primary);
  font-size: 16px;
}
</style>
